package com.export.service.Impl;

import com.export.bean.DeviceKvHistoryModel;
import com.export.rowmapper.DeviceKvHistoryRowMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by MeowParse on 2017/5/9.
 *
 */
@Repository
public class DeviceKvHistoryRepository_UNITED {

    private static final Logger logger = LoggerFactory.getLogger(DeviceKvHistoryRepository_UNITED.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    //按天查询
    @Transactional
    public List<DeviceKvHistoryModel> findDeviceKvHistory(String[] tableName,String deviceId) {
        String[] queryString = new String[24];
        for(int i = 0;i<24;i++){
            queryString[i] = "select created,device_id,property,`value` from "+tableName[i];
        }
        String resultString = String.join(" union all ", queryString);
        resultString +=" where device_id= ? order by created";
//        String queryString = "select created,deviceid,property,`value` from "+tableName+" where deviceid= ?";

        return (List<DeviceKvHistoryModel>)jdbcTemplate.query(resultString,
                new Object[]{deviceId}, new DeviceKvHistoryRowMapper());
    }

    //按小时查询
    @Transactional
    public List<DeviceKvHistoryModel> findDeviceKvHistoryByHour(String tableName,String deviceId) {
        String queryString = "select FROM_UNIXTIME(created) as created,device_id,property,`value` from "+tableName +" where device_id= ? order by created";
//        String queryString = "select created,deviceid,property,`value` from "+tableName+" where deviceid= ?";

        return (List<DeviceKvHistoryModel>)jdbcTemplate.query(queryString,
                new Object[]{deviceId}, new DeviceKvHistoryRowMapper());
    }

    //按设备ID和设备数据点查询
    @Transactional
    public List<DeviceKvHistoryModel> findDeviceHistoryByProperty(String tableName,String deviceId,String property){

        String queryString = "select FROM_UNIXTIME(created) as created,device_id,property,`value` from "+tableName +" where device_id= ? and property = ? order by created";

        return (List<DeviceKvHistoryModel>)jdbcTemplate.query(queryString,
                new Object[]{deviceId}, new DeviceKvHistoryRowMapper());
    }

    //按设备ID和设备数据点查询
    @Transactional
    public List<DeviceKvHistoryModel> findDeviceHistoryByProperty(String tableName,String[] deviceId,String[] property){

        String deviceIds = String.join(",", deviceId);
        String properties = String.join(",", property);

        logger.info(deviceIds);
        logger.info(properties);

        String queryString = "select FROM_UNIXTIME(created) as created,device_id,property,`value` from "+
                tableName +" where device_id in ("+ deviceIds +") and property IN ("+ properties +") order by created";

        return (List<DeviceKvHistoryModel>)jdbcTemplate.query(queryString,
                new Object[]{}, new DeviceKvHistoryRowMapper());
    }

}


